<?php

namespace App\Http\Controllers;

use App\Services\ALiOssService;
use App\Services\HuaWeiObsService;
use Illuminate\Http\Request;
use Obs\ObsClient;

class CommonController extends BaseController
{

    protected $ak = "VNMKYZQ8IFYQE5JWZ77U";//华为云帐号Access Key
    protected $sk = "Nwtl3FmJ2k6fPHMOPM9YGRQaOHpV0uH8CbrGW3ZZ";//华为云帐号Secret Access Key
    protected $bucket = "video-source";//对象存储的桶名称
    //直播服务应用区域和各服务的终端节点
    protected $endpoint = 'https://obs.cn-north-4.myhuaweicloud.com';
    protected $client;




    public function __construct(){
        $ak = $this->ak;
        $sk = $this->sk;
        $endpoint = $this->endpoint;

        //创建ObsClient实例
        $this->client = new ObsClient([
            'key' => $ak,
            'secret' => $sk,
            'endpoint' => $endpoint
        ]);
    }


    /**
     * 上传文件到华为OBS
     * @param $dir 文件路径 如：goodsImg/20200724/
     * @param $file 需要上传的文件资源 如 $request->file('file');
     * @param ALiOssService $ALiOssService
     * @return array
     * @throws OssException
     */
    public function uploadFileObs(Request $request, HuaWeiObsService $huaWeiObsService)
    {
        $file = $request->file('file');//需要上传的文件
        $source_file = $file->getPathname();//待上传对象的源文件路径
        $putObject = $huaWeiObsService->putObject($file);
        if ($putObject['code'] == 200){
            return $this->send(200, 'success', $putObject);
        }
        return $this->send(500, 'error');
    }


}
